package com.itxiuyixiu.homework.entity.dto;

import com.itxiuyixiu.homework.entity.enums.RoleType;
import com.itxiuyixiu.homework.entity.enums.Sex;
import com.itxiuyixiu.homework.entity.model.Academy;
import com.itxiuyixiu.homework.entity.model.Major;
import com.itxiuyixiu.homework.entity.model.Student;

import java.time.LocalDate;

/**
 * @author 黄磊
 * @date 2020/3/5
 */
public class StudentDto implements Cloneable {
    /**
     * 学号
     */
    private String studentId;

    /**
     * 密码
     */
    private String password;

    /**
     * 身份权限
     */
    private String authorities;

    /**
     * 学生姓名
     */
    private String studentName;

    /**
     * 性别
     */
    private Sex sex;

    /**
     * 生日
     */
    private LocalDate birthday;

    /**
     * 身份证
     */
    private String identityCard;

    /**
     * 入学年份
     */
    private String startYear;

    /**
     * 班级
     */
    private String studentClass;

    /**
     * 联系方式
     */
    private String studentContact;

    /**
     * 学院id
     */
    Integer academyId;

    /**
     * 专业id
     */
    Integer majorId;

    public static Student parseStudent(StudentDto dto, String studentId, String password, Academy academy, Major major) {
        dto.setStudentId(studentId);
        dto.setPassword(password);
        return parseStudent(dto, academy, major);
    }

    public static Student parseStudent(StudentDto dto, Academy academy, Major major) {
        Student student = new Student();
        student.setStudentId(dto.getStudentId());
        student.setPassword(dto.getPassword());
        student.setAuthorities(RoleType.STUDENT);
        student.setStudentName(dto.getStudentName());
        student.setSex(dto.getSex());
        student.setIdentityCard(dto.getIdentityCard());
        student.setStudentContact(dto.getStudentContact());
        student.setAcademy(academy);
        student.setMajor(major);
        student.setStudentClass(dto.getStudentClass());
        student.setStartYear(String.valueOf(LocalDate.now().getYear()));
        setBirthdayToStudent(dto.getIdentityCard(), student);
        return student;
    }

    private static void setBirthdayToStudent(String authorities, Student student) {
        String substring = authorities.substring(6, 14);
        LocalDate birthday = LocalDate.parse(substring.substring(0, 4) + "-" + substring.substring(4, 6) + "-" + substring.substring(6, 8));
        student.setBirthday(birthday);
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAuthorities() {
        return authorities;
    }

    public void setAuthorities(String authorities) {
        this.authorities = authorities;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public Sex getSex() {
        return sex;
    }

    public void setSex(Sex sex) {
        this.sex = sex;
    }

    public LocalDate getBirthday() {
        return birthday;
    }

    public void setBirthday(LocalDate birthday) {
        this.birthday = birthday;
    }

    public String getIdentityCard() {
        return identityCard;
    }

    public void setIdentityCard(String identityCard) {
        this.identityCard = identityCard;
    }

    public String getStartYear() {
        return startYear;
    }

    public void setStartYear(String startYear) {
        this.startYear = startYear;
    }

    public String getStudentClass() {
        return studentClass;
    }

    public void setStudentClass(String studentClass) {
        this.studentClass = studentClass;
    }

    public String getStudentContact() {
        return studentContact;
    }

    public void setStudentContact(String studentContact) {
        this.studentContact = studentContact;
    }

    public Integer getAcademyId() {
        return academyId;
    }

    public void setAcademyId(Integer academyId) {
        this.academyId = academyId;
    }

    public Integer getMajorId() {
        return majorId;
    }

    public void setMajorId(Integer majorId) {
        this.majorId = majorId;
    }

    public void setMajorId(int majorId) {
        this.majorId = majorId;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
